package tech.gismap.admin.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.gismap.admin.mapper.OaDeptMapper;
import tech.gismap.admin.service.OaDeptService;
import tech.gismap.admin.domain.OaDept;
import tech.gismap.admin.web.bo.OaDeptBo;
import tech.gismap.admin.web.vo.OaDeptVo;
import tech.gismap.common.core.domain.R;
import tech.gismap.common.core.utils.MapstructUtils;
import tech.gismap.common.log.annotation.Log;
import tech.gismap.common.log.enums.BusinessType;
import tech.gismap.common.mybatis.core.page.PageQuery;
import tech.gismap.common.mybatis.core.page.TableDataInfo;
import tech.gismap.common.web.core.BaseController;

import java.util.Arrays;

/**
 * 部门
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/oa/dept")
public class OaDeptController extends BaseController {

    private final OaDeptMapper baseMapper;

    private final OaDeptService baseService;

    /**
     * 删除部门
     *
     * @param ids 标识码
     */
    @DeleteMapping("/{ids}")
    @SaCheckPermission("oa:dept:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
    public R<Void> deleteByIds(@PathVariable Long[] ids) {
        return toAjax(baseMapper.deleteByIds(Arrays.asList(ids)));
    }

    /**
     * 新增部门
     */
    @PostMapping
    @SaCheckPermission("oa:dept:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    public R<Void> insert(@Validated @RequestBody OaDeptBo bo) {
        return toAjax(baseMapper.insert(MapstructUtils.convert(bo, OaDept.class)));
    }

    /**
     * 修改部门
     */
    @PutMapping
    @SaCheckPermission("oa:dept:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    public R<Void> updateById(@Validated @RequestBody OaDeptBo bo) {
        return toAjax(baseMapper.updateById(MapstructUtils.convert(bo, OaDept.class)));
    }

    /**
     * 根据部门标识码获取详细信息
     *
     * @param id 标识码
     */
    @GetMapping(value = "/{id}")
    @SaCheckPermission("oa:dept:query")
    @Log(title = "部门", businessType = BusinessType.LIST)
    public R<OaDeptVo> selectVoById(@PathVariable Long id) {
        return R.ok(baseMapper.selectVoById(id));
    }

    /**
     * 获取部门信息列表
     */
    @GetMapping("/list")
    @SaCheckPermission("oa:dept:list")
    @Log(title = "部门", businessType = BusinessType.SELECT)
    public TableDataInfo<OaDeptVo> selectPageList(OaDeptBo bo, PageQuery pageQuery) {
        return baseService.selectPageList(bo,pageQuery);
    }
}
